<?php

class Eleven_Resource_Picture extends CMS_Model_Resource_Db_Table_Abstract
{
	protected $_name = 'picture';
	protected $_primary = 'id';
	protected $_rowClass = 'Eleven_Resource_Picture_Item';
    
	protected $_referenceMap = array(
        'CatePics' => array(
            'columns'           => array('cate'),
            'refTableClass'     => 'Eleven_Resource_Catepic',
            'refColumns'        => array('id'),
            'onDelete'          => self::CASCADE,
        ),
    );
	
    public function getPictures($cateId=NULL, $paged = false, $order = null) 
    {
		$select = $this->select ();
		$select->from ( 'picture' );
		
		if (true === is_array ( $order )) 
		{
			$select->order ( $order );
		}
		
		if ($cateId)
		{
		    $select->where('cate = ?', $cateId);
		}
		
		if (null !== $paged) {
			$adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
			$count = clone $select;
			$count->reset ( Zend_Db_Select::COLUMNS );
			$count->reset ( Zend_Db_Select::FROM );
			$count->from ( 'picture', new Zend_Db_Expr ( 'COUNT(*) AS `zend_paginator_row_count`' ) );
			$adapter->setRowCount ( $count );
			
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( 9 )->setCurrentPageNumber ( ( int ) $paged );
			return $paginator;
		}
		return $this->fetchAll ( $select );
	}
	
	public function getPics($total, $order='id DESC')
	{
	    $select = $this->select();
	    $select->limit($total);
	    $select->order($order);
	    
	    return $this->fetchAll($select);
	}
	
	public function getPictureById($id)
	{
	    $id = (int) $id;
	    return $this->find ( $id )->current ();
	}
	
	public function deletePictureById($id)
	{
	    return $this->delete('id = '.$id);
	}
}

?>